package com.lin.gulimall.order.listener;

import com.lin.gulimall.common.to.mq.StockLockedTo;
import com.lin.gulimall.order.entity.OrderEntity;
import com.lin.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "stock.release.stock.queue")
@Service
public class StockCloseListener {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitHandler
    public void listener(StockLockedTo stockLockedTo, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的库存信息，准备关闭工作单" + stockLockedTo.getDetailTo());
        try {
            //TODO 持久化消息到mq_message表中，并设置消息状态为3-已抵达（保存日志记录）
            rabbitTemplate.convertAndSend("order-event-exchange", "order.release.other", stockLockedTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
